<?php
class userController extends BaseController
{
    public function loginAction(){
        $uid = cookie('uid');
        if(!empty($uid)){
            $this->redirect('index.php');
        }
        if(!empty($_POST)){
            $ob = D('user');
            $username = post('username');
            $password = post('password');
            if(empty($username) || empty($password)){
                echo return_json(-1,'请填写账号或密码');exit();
            }
            $userInfo = $ob->getUserInfoByName($username);
            if(empty($userInfo)){
                echo return_json(-2,'账号不存在');exit();
            }
            if($userInfo['password'] !== $password){
                echo return_json(-3,'密码错误');exit();
            }
            setcookie('uid',encode($userInfo['id']),time()+3600,ROOT);
            echo return_json(1,'ok');exit();
        }
        $this->display('login.html');
    }
    public function loginOutAction(){
        setcookie('uid','',time()-3600,ROOT);     
        header('location:'.APP.'/user/login.html');
    }
}